﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace nhLib.Extensions;

public static class StringExtension
{
    /// <summary>
    /// 获取字符串的SHA256
    /// </summary>
    /// <param name="pwd">字符串</param>
    /// <returns>SHA256的结果</returns>
    public static string GetPwdHash(this string pwd)
    {
        return Convert.ToBase64String(SHA256.HashData(Encoding.Default.GetBytes(pwd)));
    }

    /// <summary>
    /// 去掉每行的空行
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static string MultiLineTrim(this string source)
    {
        string[] str = source.Split('\n');

        for (int i = 0; i < str.Length; i++)
        {
            str[i] = str[i].Trim();
        }

        string outStr = string.Join('\n', str);
        return outStr;
    }

    /// <summary>
    /// ""到Null
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static string? EmptyToNull(this string source)
    {
        return source.Trim() == "" ? null : source.Trim();
    }
}
